package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseMaintenanceAddDto;
import com.kaili.basic.pojo.dto.BaseMaintenanceDto;
import com.kaili.basic.pojo.dto.BaseMaintenanceUpdateDto;
import com.kaili.basic.pojo.entity.BaseMaintenance;
import com.kaili.basic.service.BaseMaintenanceService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by renzhikun on 18-6-28.
 */
@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "维修保养管理", description = "维修保养管理相关API")
public class BaseMaintenanceController {
    @Autowired
    private BaseMaintenanceService maintenanceService;

    @PostMapping("/maintenances")
    @ApiOperation(value = "新增", notes = "新增维修保养数据")
    public RestStatus addBaseMaintenance(@Validated @RequestBody BaseMaintenanceAddDto baseMaintenanceAddDto) {
        if (baseMaintenanceAddDto == null) {
            return CommonStatus.BAD_REQUEST;
        }
        maintenanceService.addBaseMaintenance(baseMaintenanceAddDto);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/maintenances/{maintenanceId}")
    @ApiOperation(value = "删除", notes = "删除维修保养数据")
    public RestStatus deleteBaseMaintenance(@PathVariable("maintenanceId") String maintenanceId) {
        if (maintenanceId == null) {
            return CommonStatus.BAD_REQUEST;
        }
        maintenanceService.deleteBaseMaintenance(Long.valueOf(maintenanceId));
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/maintenances")
    @ApiOperation(value = "更新", notes = "更新维修保养数据")
    public RestStatus updateBaseMaintenance(@Validated @RequestBody BaseMaintenanceUpdateDto baseMaintenanceupDto) {
        if (baseMaintenanceupDto != null) {
            maintenanceService.updateBaseMaintenance(baseMaintenanceupDto);
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @GetMapping("/maintenances")
    @ApiOperation(value = "分页查询维修保养数据", notes = "分页查询事件")
    public Page<BaseMaintenance> queryBaseMaintenance(@ModelAttribute BaseMaintenanceDto baseMaintenanceDto) {
        BaseMaintenance baseMaintenance = new BaseMaintenance();
        BeanUtils.copyProperties(baseMaintenanceDto, baseMaintenance);
        if(LoginDetailUtils.getSiteId()!=1)
            baseMaintenance.setSiteId(LoginDetailUtils.getSiteId().longValue());
        return maintenanceService.queryBaseMaintenance(new Page<BaseMaintenance>(baseMaintenance.getPage(), baseMaintenance.getPageSize()), baseMaintenance);
    }
}
